import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:super_tool_server/common/utils/color_util.dart';
import 'package:super_tool_server/component/edit_text_field/edit_text_field.dart';

///输入框的圆角样式
enum CustomTextFieldShape {
  circle, // 圆角  手机 & PC : small样式 4 normal样式 6
  round, // 方形  手机 & PC : 0.0
}

///输入框的大小
enum CustomTextFieldSize {
  large, //    PC：高度 100   字号 16
  medium, //    PC：高度 80   字号 16
  normal, // 手机： 高度 52  字号 16   PC：高度 50   字号 16
  small, //  手机： 高度 36  字号 15   PC: 高度 36   字号 14
}

enum CustomTextFieldStyle {
  normal, //白色背景  hover： border color 紫色
  error, //白色背景  hover： border color 红色
  transparent, //透明
}

enum CustomTextFieldMode {
  large, //大屏适配
  small, //小屏适配
}

enum CustomTextFieldAlignment {
  start,
  center,
}

/// 自定义文本输入框
class CustomTextField extends StatefulWidget {
  const CustomTextField({
    required this.customTextFieldStyle,
    required this.customTextFieldSize,
    required this.customTextFieldShape,
    required this.customTextFieldMode,
    this.leadingWidget,
    this.leadingText,
    this.trailingWidget,
    this.hintText,
    this.hintStyle,
    this.maxLength = TextField.noMaxLength,
    this.maxLine = 1,
    this.controller,
    this.defaultText = '',
    this.focusNode,
    this.isShowDeleteButton = true,
    this.autoFocus = false,
    this.obscureText = false,
    this.hasBottomLine = false,
    this.enable = true,
    this.enableInteractiveSelection = true,
    this.onTap,
    this.onChanged,
    this.onEditingComplete,
    this.onSubmitted,
    this.inputFormatterList,
    this.keyboardType,
    this.outPadding,
    this.alignment = CustomTextFieldAlignment.center,
    this.border,
    Key? key,
  }) : super(
          key: key,
        );

  /// 左侧组件
  final Widget? leadingWidget;

  ///左侧文案，优先级低于 leadingWidget
  final String? leadingText;

  /// 右侧组件
  final Widget? trailingWidget;

  ///提示信息
  final String? hintText;

  ///提示文字样式
  final TextStyle? hintStyle;

  final String defaultText;

  ///字数限制 默认是无限输入
  final int maxLength;

  ///行数限制 默认是1
  final int maxLine;

  ///是否显示删除按钮
  final bool isShowDeleteButton;

  ///是否可编辑
  final bool enable;

  ///是否自动获取焦点
  final bool autoFocus;

  /// 是否是密码输入
  final bool obscureText;

  ///收否可以交互选择
  final bool enableInteractiveSelection;

  /// 控制器
  final TextEditingController? controller;

  /// 焦点
  final FocusNode? focusNode;

  ///编辑的text的样式
  final List<TextInputFormatter>? inputFormatterList;

  ///输入框样式
  final CustomTextFieldStyle customTextFieldStyle;

  ///输入框大小
  final CustomTextFieldSize customTextFieldSize;

  /// 输入框圆角样式
  final CustomTextFieldShape customTextFieldShape;

  /// 适配模式（大屏、小屏）
  final CustomTextFieldMode customTextFieldMode;

  ///点击回调函数
  final GestureTapCallback? onTap;

  /// 输入内容改变回调
  final ValueChanged<String>? onChanged;

  /// 输入完毕回调
  final VoidCallback? onEditingComplete;

  /// 提交输入回调
  final ValueChanged<String>? onSubmitted;

  ///键盘的类型
  final TextInputType? keyboardType;

  ///是否显示底部的分割线
  final bool hasBottomLine;

  final CustomTextFieldAlignment alignment;

  /// 输入框外边距
  final EdgeInsets? outPadding;

  final Border? border;

  @override
  _CustomTextFieldState createState() => _CustomTextFieldState();
}

class _CustomTextFieldState extends State<CustomTextField> {
  late bool _isHovering;
  late TextEditingController _controller;

  @override
  void initState() {
    _isHovering = false;
    // widget?.focusNode?.addListener(() {
    //   if (_isHovering != widget.focusNode.hasFocus) {
    //     _isHovering = widget.focusNode.hasFocus;
    //     setState(() {});
    //   }
    // });
    super.initState();
    _controller = widget.controller ?? TextEditingController();
    _controller.text = widget.defaultText;
  }

  @override
  void didUpdateWidget(covariant CustomTextField oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.defaultText != oldWidget.defaultText && widget.defaultText != _controller.text) {
      _controller.text = widget.defaultText;
    }
  }

  double _height(BuildContext context) {
    if (widget.customTextFieldMode == CustomTextFieldMode.large) {
      if (widget.customTextFieldSize == CustomTextFieldSize.large) {
        55;
      }
      if (widget.customTextFieldSize == CustomTextFieldSize.medium) {
        return 40;
      }
      if (widget.customTextFieldSize == CustomTextFieldSize.normal) {
        return 50;
      }
      if (widget.customTextFieldSize == CustomTextFieldSize.small) {
        return 36;
      }
    }
    if (widget.customTextFieldMode == CustomTextFieldMode.small) {
      if (widget.customTextFieldSize == CustomTextFieldSize.normal) {
        return 52;
      }
      if (widget.customTextFieldSize == CustomTextFieldSize.small) {
        return 36;
      }
    }

    return 36;
  }

  TextStyle _textStyle(BuildContext context) {
    // print(widget.customTextFieldStyle);
    // print(widget.customTextFieldSize);
    if (widget.customTextFieldStyle == CustomTextFieldStyle.transparent ||
        widget.customTextFieldStyle == CustomTextFieldStyle.error) {
      return const TextStyle(
        fontSize: 14,
        color: Colors.white,
      );
    }
    if (widget.customTextFieldMode == CustomTextFieldMode.large) {
      if (widget.customTextFieldSize == CustomTextFieldSize.small) {
        return const TextStyle(
          fontSize: 14,
          color: Color(0xFF333333),
        );
      }
      if (widget.customTextFieldSize == CustomTextFieldSize.normal) {
        return const TextStyle(
          fontSize: 16,
          color: Color(0xFF333333),
        );
      }
      if (widget.customTextFieldSize == CustomTextFieldSize.large ||
          widget.customTextFieldSize == CustomTextFieldSize.medium) {
        return const TextStyle(
          fontSize: 24,
          color: Colors.black,
        );
      }
    }

    if (widget.customTextFieldMode == CustomTextFieldMode.small) {
      if (widget.customTextFieldSize == CustomTextFieldSize.small) {
        return const TextStyle(
          fontSize: 14,
          color: Color(0xFF333333),
        );
      }

      if (widget.customTextFieldSize == CustomTextFieldSize.normal) {
        return const TextStyle(
          fontSize: 14,
          color: Color(0xFF333333),
        );
      }
    }

    return const TextStyle(
      fontSize: 14,
      color: Color(0xFF333333),
    );
  }

  ///12
  EdgeInsets _inputPadding(BuildContext context) {
    return EdgeInsets.zero;
  }

  ///外部container的padding
  EdgeInsets _outPadding(BuildContext context) {
    if (widget.outPadding != null) {
      return widget.outPadding!;
    }
    return EdgeInsets.symmetric(
      horizontal: 8,
      vertical: widget.alignment == CustomTextFieldAlignment.start ? 5 : 0,
    );
  }

  double _borderRadius(BuildContext context) {
    if (widget.customTextFieldShape == CustomTextFieldShape.circle) {
      if (widget.customTextFieldSize == CustomTextFieldSize.large ||
          widget.customTextFieldSize == CustomTextFieldSize.medium) {
        return 5.0;
      }
    }
    return 2.0;
  }

  Color _borderColor(BuildContext context) {
    if (widget.customTextFieldStyle == CustomTextFieldStyle.error) {
      if (kIsWeb) {
        return const Color(0xFFE74719);
      }
    }
    if (widget.customTextFieldStyle == CustomTextFieldStyle.normal ||
        widget.customTextFieldStyle == CustomTextFieldStyle.transparent) {
      if (_isHovering && kIsWeb) {
        return green_dfff01;
      }
      return const Color(0xFF979797);
    }
    return Colors.transparent;
  }

  double _borderWidth(BuildContext context) {
    return 1.0;
  }

  Color _backgroundColor(BuildContext context) {
    if (widget.customTextFieldStyle == CustomTextFieldStyle.normal) {
      return Colors.white;
    }
    if (widget.customTextFieldStyle == CustomTextFieldStyle.error) {
      return Colors.transparent;
    }
    if (widget.customTextFieldStyle == CustomTextFieldStyle.transparent) {
      return Colors.transparent;
    }

    return Colors.white;
  }

  Widget? _buildLeadingWidget(BuildContext context) {
    if (widget.leadingWidget != null) {
      return widget.leadingWidget;
    }
    if (widget.leadingText != null && widget.leadingText!.isNotEmpty) {
      return Padding(
        padding: const EdgeInsets.only(
          right: 8,
        ),
        child: Text(
          widget.leadingText!,
          style: _textStyle(context),
        ),
      );
    }
    return const SizedBox();
  }

  Widget? _buildTrailingWidget(BuildContext context) {
    if (widget.trailingWidget != null) {
      return widget.trailingWidget;
    }
    return const SizedBox();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          height: _height(context),
          padding: _outPadding(context),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(
                _borderRadius(context),
              ),
            ),
            border: widget.border ??
                Border.all(
                  color: _borderColor(context),
                  width: _borderWidth(context),
                ),
            color: _backgroundColor(context),
          ),
          child: Row(
            crossAxisAlignment: widget.alignment == CustomTextFieldAlignment.start
                ? CrossAxisAlignment.start
                : CrossAxisAlignment.center,
            children: [
              _buildLeadingWidget(context)!,
              Expanded(
                child: Padding(
                  padding: _inputPadding(context),
                  child: EditTextField(
                    controller: _controller,
                    textStyle: _textStyle(context),
                    focusNode: widget.focusNode,
                    hintStyle: widget.hintStyle ??
                        _textStyle(context).copyWith(
                          color: const Color(0xFF666666),
                        ),
                    hintText: widget.hintText,
                    onTap: widget.onTap,
                    onSubmitted: widget.onSubmitted,
                    onChanged: widget.onChanged,
                    onEditingComplete: widget.onEditingComplete,
                    inputFormatters: widget.inputFormatterList,
                    maxLength: widget.maxLength,
                    maxLines: widget.maxLine,
                    cursorColor: const Color(0xFF564BFF),
                    keyboardType: widget.keyboardType,
                    isShowDeleteButton: widget.isShowDeleteButton,
                    autoFocus: widget.autoFocus,
                    obscureText: widget.obscureText,
                    enabled: widget.enable,
                    enableInteractiveSelection: widget.enableInteractiveSelection,
                    onHover: (bool isHovering) {
                      _isHovering = isHovering;
                      setState(() {});
                    },
                  ),
                ),
              ),
              _buildTrailingWidget(context)!,
            ],
          ),
        ),
        if (widget.hasBottomLine)
          Positioned(
            left: 8,
            right: 0,
            bottom: 0,
            child: Container(
              height: 1.0,
              color: const Color(0xFFF2F2F2),
            ),
          )
      ],
    );
  }
}
